import { AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from "axios";
import { useAuthWithout } from "~/store/modules/auth";

const config: AxiosRequestConfig = {
    baseURL: import.meta.env.VITE_AXIOS_BASE_URL,
    headers: { 'Content-Type': 'application/json' },
}

const requestInterceptor = (config: AxiosRequestConfig) => {
    const auth = useAuthWithout();
    if (auth.token) {
        (config.headers as AxiosRequestHeaders).Authorization = auth.token;
    }
    return config;
}

const responseInterceptor = (response: AxiosResponse) => {
    if (response.headers.Authorization) {
        const auth = useAuthWithout();
        auth.setToken(response.headers.Authorization);
    }
    return response;
}

export {
    config, requestInterceptor, responseInterceptor
};